package com.bungarus.busi;

import com.bungarus.Busi;
import com.bungarus.codec.protobuf.MessageProtobuf;

/**
 * Created by tang on 2018/11/30.
 */
public class GroupMessageProcessing extends BusiProcessing {
    private static GroupMessageProcessing groupMessageProcessing = new GroupMessageProcessing();
    private static Busi busiServer;

    public GroupMessageProcessing() {
    }

    public static GroupMessageProcessing getInstance(Busi busiServer) {
        GroupMessageProcessing.busiServer = busiServer;
        return groupMessageProcessing;
    }

    /**
     * send message to all members in the group
     * @param o message to be sent
     * @throws BusiProcessingException
     */
    @Override
    public void doBusiness(Object o) throws BusiProcessingException {
        if(o instanceof MessageProtobuf.Message) {
            final MessageProtobuf.Message message = (MessageProtobuf.Message)o;
            //get all members in group
            BusiProcessing.busiTaskPool.submit(() -> {
                GroupSender groupSender = GroupSender.getInstance();
                groupSender.sendToGroup(message.getHeader().getTo(), message, GroupMessageProcessing.busiServer);
            });
        }
    }
}
